package lanqiao;

import java.util.LinkedList;

public class B {
    public static void main(String[] args) {
        int num = 16;
//        System.out.println(getSum(num));
//        System.out.println("to 8 "+to8(num));
//        System.out.println("to 2 "+to2(num));
//        System.out.println("to 16 "+is16(num));

        int count = 0;
        num = 1;
        while(count<2023) {
            boolean flag8 = num%getSum(to8(num))==0;
            boolean flag2 = num%getSum(to2(num))==0;
            boolean flag16 = num%is16(num)==0;
            boolean flag10 = num%getSum(num)==0;
            if (flag2&&flag8&&flag16&&flag10) {
                count++;
                System.out.println(num);
                if (count == 20) {
                    break;
                }
            }
            num++;
        }

        System.out.println("result="+num);

    }

    public static int getSum(int num) {
        int sum = 0;
        while(num>0) {
            int x = num%10;
            sum+=x;
            num = num/10;
        }
        return sum;
    }

    public static int to8(int num) {
        int num8 = 0;
        LinkedList<Integer> list = new LinkedList<>();
        while(num>0) {
            int x = num % 8;
            list.add(x);
            num = num/8;
        }
        while(list.size()>0) {
            num8 = num8*10+list.removeLast();
        }
        return num8;
    }

    public static int to2(int num) {
        int num2 = 0;
        LinkedList<Integer> list = new LinkedList<>();
        while(num>0) {
            int x = num % 2;
            list.add(x);
            num = num/2;
        }
        while(list.size()>0) {
            num2 = num2*10+list.removeLast();
        }
        return num2;
    }

    public static int is16(int num) {
        int num16 = 0;
        LinkedList<Integer> list = new LinkedList<>();
        while(num>0) {
            int x = num % 16;
            list.add(x);
            num = num/16;
        }

        int sum = 0;
        while(list.size()>0) {
            sum+=list.removeLast();
        }
        return sum;
    }
}
